<?
class Tree
{
	
	public function __get($var)	{	return get_instance()->$var; }
	public function __construct()
	{
		set_time_limit ( 5 );
	}
	
	public function rows_to_tree($rows, $id_name, $parent_name)
	{
		$output = array();
		$all = array();
		$dangling = array();
		// Initialize arrays
		foreach ($rows as $entry) {
			$entry['children'] = array();
			$id = $entry[$id_name];
		
			// If this is a top-level node, add it to the output immediately
			if ($entry[$parent_name] == 0) {
				$all[$id] = $entry;
				$output[] =& $all[$id];
		
			// If this isn't a top-level node, we have to process it later
			} else {
				$dangling[$id] = $entry; 
			}
		}
		
        $max = 10;
        $current = 0;
		// Process all 'dangling' nodes
		while (count($dangling) > 0) {
			foreach($dangling as $entry) {
				$id = $entry[$id_name];
				$pid = $entry[$parent_name];
		
				// If the parent has already been added to the output, it's
				// safe to add this node too
				if (isset($all[$pid])) {
					$all[$id] = $entry;
					$all[$pid]['children'][] =& $all[$id]; 
					unset($dangling[$entry[$id_name]]);
				}
			}
		}
		
		return $output;				
	}
	
	public function tree_to_options($tree, $item_value = '', $item_key = '', $intend = '- ', $level = 0)
	{
		$options = array();
		foreach($tree as $item)
		{		
			$option = '';
			if($level > 0)
			{
				for($i = 0; $i < $level; $i++)
				{
					$option .= $intend;
				}
			}
			$option .= $item[$item_value];
			
			//$option = '<option value="' . $item_key . '">' . $option . '</option>';
			$options[] = array('name'   => $option,
							   'id'		=> $item[$item_key]);
			
			if(isset($item['children']) && !empty($item['children']))
			{
				$options = array_merge($options, $this->tree_to_options($item['children'], $item_value, $item_key, $intend, $level + 1));
			}
		}
		return $options;
	}
	

	// traverse($pages, $page_id, $page_parent, 0
	public function traverse(&$rows, $id_name, $parent_id_name, $parent_id = NULL)
	{
		
		$children = array();
		if(empty($rows))
			return $children;
		
		for($i = 0; $i < count($rows); $i++)
		{
			//print_r($rows[$i]);
			if($parent_id == $rows[$i][$parent_id_name])
			{
				echo $rows[$i]['page_name'] . ' paremt ';
				$children[] = $rows[$i];
				
			}
		}
		
		$total_children = count($children);
		if($total_children != 0)
		{
			for($i = 0; $i < $total_children; $i++)
			{
				echo ' child ';
				$children[$i]['items'] = $this->traverse($rows, $id_name, $parent_id_name, $children[$i][$id_name]);
			}
		}
		return $children;
	}
	
	
	function get_menu_structure($m_menu_id = NULL, $language = NULL)
	{		

		$options = array('sort_by' => 'm_order');
		if(!is_null($m_menu_id))
			$options['m_menu_id'] = $m_menu_id;
		
		if(!is_null($language))
			$options['m_language'] = $language;
			
		$items = $this->get($options);

		$menu = $this->dom($items, 0);
		return $menu;
	}
	
	private function dom(&$items, $parent_id = NULL)
	{
		$children = array();
		if(empty($items))
			return $children;
			
		for($i = 0; $i < count($items); $i++)
		{	
	
			if($parent_id == $items[$i]['m_m_id'])
			{
				$children[] = $items[$i];
				unset($items[$i]);
				$items = array_values($items);	
				$i--;			
			}
		}
	
		$total_children = count($children);
		if($total_children !=0)
		{
			for($i = 0; $i < $total_children; $i++)
			{				
				$children[$i]['items'] = $this->dom($items, $children[$i]['m_id']);
			}
		}
		return $children;
	}
}